<script>
	import {
		mapState,
		mapMutations,
		} from 'vuex'
	export default {
		computed: mapState(['hasLogin','userInfo']),
		methods: {
			...mapMutations(['login']),
			checkAuth() {
				return new Promise((resolve,reject) => {
					uni.getStorage({
						key:'userInfo',
						success: (res) => {
							resolve(res.data)
						},
						fail: (err) => {
							console.log('读取储藏信息失败:',err);
							resolve(false)
						}
					});
				});
			},
			verifyTokenRequest(token) {
				//console.log('url:',getApp().globalData.baseUrl + '/api/user/verifyToken')
			  return new Promise((resolve, reject) => {
			    uni.request({
			      url: getApp().globalData.baseUrl + '/api.system/verifyToken', // 替换为你的验证接口
			      method: 'POST',
			      data: {
			        'token': token
			      },
			      success:(res) => {
					  //console.log('000：',res)
					  resolve(res)
				  } ,
			      fail: reject
			    });
			  });
			},
		},
		globalData: {
			//baseUrl: 'http://172.168.3.11',
			baseUrl: 'https://gms.lzwy.net',
		},
		async onLaunch() {
			try {
				const res  = await this.checkAuth();
				console.log('启动时判断的Res：',res)
				if(res){
					//判断获得的Token是否合规
					// 向服务端验证Token
					const res1 = await this.verifyTokenRequest(res.token);
					console.log('verfityToken:',res1);        
					if (res1.statusCode === 200) {
					   this.login(res);
					   uni.reLaunch({
					   	url: '/pages/index/index'
					   }); 
					}
					else
					{
						console.log('启动时checkAuth statusCode！=200')
						uni.reLaunch({
							url: '/pages/login/login'
						});
					}
				}
				else{
					console.log('启动时checkAuth失败，login')
					uni.reLaunch({
						url: '/pages/login/login'
					});
				}
			} catch(error) {
				console.log('启动校验失败:',error);
				uni.reLaunch({
					url:'/pages/login/login'
				});
			} 
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
	@import './common/uni.css';
</style>
